package com.ss.android.common.dialog;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import com.bytedance.common.utility.collection.WeakHandler.Callback;
import com.bytedance.common.utility.collection.WeakHandler;
import java.lang.ref.WeakReference;

public abstract class WindowBuilder implements DialogInterface, View.OnKeyListener, View.OnTouchListener, Callback {
    private final boolean a;
    private boolean b;
    private WeakHandler c;
    IBinder o;
    public WindowBase p;
    public ViewGroup q;
    WeakReference r;
    Runnable s;
    public WeakReference t;

    public WindowBuilder(Activity arg2) {
        this(arg2.getWindow().getDecorView());
    }

    private WindowBuilder(View arg4) {

        this.c = new WeakHandler(((Callback)this));
        boolean v0 = arg4 == null ? true : false;
        this.a = v0;
        IBinder v0_1 = this.a ? null : arg4.getWindowToken();
        this.o = v0_1;
        if(this.o == null && !this.a) {
            this.r = new WeakReference(arg4);
            arg4.getViewTreeObserver().addOnGlobalLayoutListener(new k(this));
        }

        if(arg4 != null) {
            this.t = new WeakReference(arg4.getContext());
        }

        this.p = this.c();
        if(this.p == null) {
            throw new NullPointerException("getWindowBase() can\'t return null");
        }

        this.q = this.b();
        if(this.q == null) {
            throw new NullPointerException("getRootView() can\'t return null");
        }

        this.q.setFocusableInTouchMode(true);
        this.q.setOnKeyListener(((View.OnKeyListener)this));
        this.q.setOnTouchListener(((View.OnTouchListener)this));
    }

    private void a() {
        this.c.sendEmptyMessage(67);
        WindowBase v0 = this.p;
        if((v0.d) && !v0.b() && v0.a != null && v0.c != null) {
            try {
                v0.a.removeViewImmediate(v0.c);
                v0.d = false;
            }
            catch(Exception v0_1) {
            }
        }
    }

    public final View a(int arg2) {
        return this.q.findViewById(arg2);
    }

    public void a(Bundle arg1) {
    }

    public abstract ViewGroup b();

    public abstract WindowBase c();

    public void cancel() {
        this.c.sendEmptyMessage(68);
        this.a();
    }

    public final void d() {
        if((this.a) || this.o != null) {
            this.e();
            this.p.a(this.q, this.o);
            this.f();
        }
        else {
            this.s = new l(this);
        }
    }

    public void dismiss() {
        this.a();
    }

    protected final void e() {
        if(!this.b && !this.b) {
            this.a(null);
            this.b = true;
        }
    }

    protected final void f() {
        this.c.sendEmptyMessage(69);
    }

    public void handleMsg(Message message) {
    }

    public boolean onKey(View arg2, int arg3, KeyEvent arg4) {
        boolean v0;
        if(arg3 != 4 || arg4.getAction() != 0) {
            v0 = false;
        }
        else {
            this.a();
            v0 = true;
        }

        return v0;
    }

    public boolean onTouch(View arg2, MotionEvent arg3) {
        return false;
    }
}

